<?php

class BasedArrayToXML {
    const _START  = '<';
    const _BACK   = '</';
    const _END    = '>';
    const _HEADER = '<?xml version="1.0" encoding="utf-8"?>';
    const _OAH    = "\n";
    private static $instance;

    //construct
    private function __construct() {

    }
    
    //instance
    static public function getInstance() {
        if (!isset(self::$instance)) {
            $class = __CLASS__;
            self::$instance = new $class();
        }
        return self::$instance;
    }
    
    //getXML
    public function getXML($array) {
        if (empty($array)) return NULL;
        $result = self::_HEADER.self::_OAH;
        $result .= $this->make($array);
        return $result;
    }

    public function make($data,$keys="") {
        $body = ""; 
        if (empty($data) || !is_array($data)) return NULL;
        foreach((array)$data as $key=>$val) {
            if (!is_array($val) && !empty($val)) {
                $body .= self::_START.$key.self::_END.$val.self::_BACK.$key.self::_END.self::_OAH;
            }
            else {
                $body .= self::_START.$key.self::_END.self::_OAH;
                $body .= $this->make($val,$key);
            }
        }
        if (!empty($keys)) {
            $body .= self::_BACK.$keys.self::_END.self::_OAH;
        }
        return $body;
    }

}

?>

